/*
 *      OneOperandILToken.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

package ilanguage.iltokens.operations;

import ilanguage.iltokens.IL_SSA;
import lexer.EntryType;


/**
 * The Class OneOperandILToken.
 */
public abstract class OneOperandIL_SSA extends OperationIL_SSA {

	/** The first arg. */
	protected IL_SSA firstArg;

	/** The result register. */
	private String resultRegister="";

	/**
	 * Instantiates a new one operand il token.
	 *
	 * @param token the token
	 */
	public OneOperandIL_SSA(IL_SSA token)
	{
		super();
		this.setFirstArg(token);
	}

	/**
	 * Instantiates a new one operand il token.
	 *
	 * @param cond the cond
	 * @param b the b
	 */
	public OneOperandIL_SSA(final IL_SSA cond, final boolean b) {
		super();
		this.setFirstArg(cond);
	}

	/**
	 * Gets the first arg.
	 *
	 * @return the first arg
	 */
	public IL_SSA getFirstArg() {
		return firstArg;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ILanguage.OperationToken#GetPrefix()
	 */
	public String getPrefix() {
		if (getFirstArg() == null)
			return super.getPrefix() + ",null";
		return super.getPrefix() + "," + getFirstArg().getValue();
	}


	/**
	 * Gets the result register.
	 *
	 * @return the result register
	 */
	public String getResultRegister() {
		return resultRegister;
	}

	@Override
	public boolean isDiscartable() {
		return discartable(this.getFirstArg());
	}

	/**
	 * Register assign.
	 *
	 * @param R the r
	 */
	public void RegisterAssign(final String R) {
		resultRegister = R;
	}

	/**
	 * Sets the first arg.
	 *
	 * @param t the new first arg
	 */
	public void setFirstArg(IL_SSA t) {
		firstArg = t;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ILanguage.IL_Token#SetReference(ILanguage.IL_Token)
	 */
	public void setReference(IL_SSA t) {
		this.setFirstArg(t);
	}

	/**
	 * Sets the result register.
	 *
	 * @param myResultRegister the new result register
	 */
	public void setResultRegister(String myResultRegister) {
		resultRegister = myResultRegister;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ILanguage.IL_Token#SolveTypes()
	 */
	public EntryType solveTypes() {
		return this.getFirstArg().solveTypes();
	}

	public String verificarOverFlowDOUBLE() {
		String codigo="";
		/*codigo+="\t\tfld maxdouble \n";
		codigo+="\t\tfcomp \n";*/
		codigo+="\t\tfstsw FlagVar\n";
		codigo+="\t\tmov ax, FlagVar\n";
		codigo+="\t\tsahf \n";
		codigo+="\t\tcall doubleoverflow\n";

		return codigo;

	}

	public String verificarOverFlowINT() {
		String codigo="";
		/*codigo+="\t\tcmp maxint , "+this.getResultRegister()+"\n";*/
		codigo+="\t\tcall intoverflow\n";
		return codigo;
	}

	public String verificarUnderFlowDOUBLE() {
		String codigo="";
		/*codigo+="\t\tfld mindouble \n";
		codigo+="\t\tfcomp \n";*/
		codigo+="\t\tfstsw FlagVar\n";
		codigo+="\t\tmov ax, FlagVar\n";
		codigo+="\t\tsahf \n";
		codigo+="\t\tcall doubleunderflow\n";

		return codigo;

	}

	public String verificarUnderFlowINT() {
		String codigo="";
		/*codigo+="\t\tcmp minint , "+this.getResultRegister()+"\n";*/
		codigo+="\t\tcall intunderflow\n";
		return codigo;
	}

}
